package com.lc.bean;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        HashMap<String,Object> kk = new HashMap<>();
        kk.put(",",3);
        MyHash map = new MyHash();
        map.put("kk",1);
        map.put("mm",2);
        System.out.println(map.get("kk")+","+map.get("mm"));

//        String s = "s";
//        System.out.println(new Date().getTime());
//        for(int i=0;i<300000L;i++){
//            s+="m";
//        }
//        System.out.println(new Date().getTime());
    }
}

class MyHash{
    private Node[] space = new Node[1024];

    public void put(String key,Object value){
       int  code =   key.hashCode();//md5,sha1
       int index =  (space.length-1) & code;
        Node node = new Node();
        node.key = key;
        node.value = value;
       if(space[index]==null){
           space[index] = node;
       }else{
          Node headNode =   space[index];
          Node lastNode  =  findLast(headNode);
          lastNode.next = node;
       }
    }
    private Node findLast(Node head){
        while(head!=null){
            if(head.next==null)
                return head;
            else
                head = head.next;
        }
        return null;
    }
    private Node findByKey(Node head,Object key){
        while(head!=null){
            if(head.key.equals(key))
                return head;
            else
                head = head.next;
        }
        return null;
    }

    public Object get(String key) {
        int code = key.hashCode();
        int index = space.length-1 & code;
        if(space[index]!=null){
            Node headNode=  space[index];
           return findByKey(headNode,key).value;
        }
        return null;
    }

}
class Node{
    public Object key;
    public Object value;
    public Node next;

}
